package com.hee.erp.modules.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hee.erp.annotation.Log;
import com.hee.erp.common.R;
import com.hee.erp.constant.Const;
import com.hee.erp.filter.LoginContextHolder;
import com.hee.erp.modules.entity.*;
import com.hee.erp.modules.service.*;
import com.hee.erp.pojo.LoginInfo;
import com.hee.erp.pojo.SysUserDetail;
import com.hee.erp.util.JwtUtil;
import com.hee.erp.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author weizehong
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/modules/sysUser")
@Api(tags = "sysUser",produces = "application/json")
@Slf4j
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    ISysUserContractService sysUserContractService;

    @Autowired
    ISysUserCertificateService sysUserCertificateService;

    @Autowired
    ISysUserIdcardService sysUserIdcardService;

    @Autowired
    ISysUserHealthReportService sysUserHealthReportService;

    @Autowired
    ISysUserRemarkService sysUserRemarkService;

    @Autowired
    ISysUserGrowLogService sysUserGrowLogService;

    @Autowired
    ISysUserLeaveInfoService sysUserLeaveInfoService;

    @Autowired
    ISysUser2RightService sysUser2RightService;
//
//
//    @PostMapping()
//    @ApiOperation(value = "insert",notes = "这里是参数说明")
//    @Log("新建档案")
//    public R<Boolean> add(@RequestBody SysUserVo vo){
//        sysUserService.addUser(vo);
//
//        return R.success(true);
//    }

    @GetMapping("/delById/{id}")
    @ApiOperation(value = "delById",notes = "这里是参数说明")
    @Log("删除档案")
    public R<Boolean> delById(@PathVariable String id){
        sysUserService.removeById(id);
        return R.success(true);
    }

    @PostMapping("/saveUserBasicInfo")
    @ApiOperation(value = "saveUserBasicInfo",notes = "这里是参数说明")
    @Log("新建/修改-档案基础信息")
    public R<Boolean> saveUserBasicInfo(@RequestBody SysUser sysUser){
        sysUserService.checkClockRepeat(sysUser);
        sysUserService.updateById(sysUser);

        return R.success(true);
    }

    @GetMapping("/getAllUserListExceptSelf")
    @ApiOperation(value = "获取除自己之外的所有人员",notes = "")
    public R getAllUserListExceptSelf(){
        List<SysUser> userList = sysUserService.getAllUserListExceptSelf();
        return R.success(userList);
    }

    @GetMapping("/getUserDetail/{id}")
    @ApiOperation(value = "getUserDetail",notes = "这里是参数说明")
    @Log("获取档案详情")
    public R<SysUserDetail> getUserDetail(@PathVariable String id){
        if(id.equals("0")){
            id = LoginContextHolder.get().getUserId();
        }
        SysUserDetail userDetail = sysUserService.getDetailById(id);
        return R.success(userDetail);
    }

    @GetMapping("/getById")
    @ApiOperation("getById")
    public R getById(@RequestParam(value = "id",required = true) String id){
        SysUserDetail entity = sysUserService.getDetailById(id);
        return R.success(entity);
    }

    @PostMapping("/saveUserDetail")
    @ApiOperation(value = "saveUserDetail",notes = "这里是参数说明")
    @Log("保存我的档案")
    public R<SysUserDetail> saveUserDetail(@RequestBody SysUserDetail sysUserDetail){
        sysUserService.saveUserDetail(sysUserDetail);
        return R.success(sysUserDetail);
    }



    @PostMapping("/listByPage")
    @ApiOperation(value = "listByPage",notes = "这里是参数说明")
    @Log("读取档案列表")
    public R<IPage<SysUser>> listByPage(@RequestBody UserListPageParamsVo vo){
        IPage<SysUser> userPage = sysUserService.listByPage(vo);
        return R.success(userPage);
    }

    @PostMapping("/test112")
    @ApiOperation(value = "test112")
    public Boolean test(){

        return true;
    }

    @PostMapping("/login")
    @ApiOperation(value = "login")
    @Log("登录")
    public R<JSONObject> login(@RequestBody SysUserLoginVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUser user = sysUserService.login(vo);
        String token = JwtUtil.createToken(user.getId(), user.getDbKey());
        response.setHeader(Const.RESPONSE_HEADER_TOKEN, token);

        JSONObject result = new JSONObject();
        result.put("userId",user.getId());
        result.put("userName",user.getUserName());
        result.put("identificationPhoto",user.getIdentificationPhoto());

        //保存登录信息到本地线程
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserId(user.getId());
        loginInfo.setUserDbKey(user.getDbKey());
        loginInfo.setExpireTimeMillis(System.currentTimeMillis() + JwtUtil.EXPIRATION);
        loginInfo.setRequest(request);
        LoginContextHolder.set(loginInfo);

        return R.success(result);
    }

    @GetMapping("/checkTelExists")
    @ApiOperation(value = "checkTelExists")
    public R<Boolean> checkTelExists(String userTel){
        Boolean result = sysUserService.checkTelExists(userTel);
        return R.success(result);
    }

    @GetMapping("/checkUserNoExists")
    @ApiOperation(value = "checkUserNoExists")
    public R<Boolean> checkUserNoExists(String userNo){
        Boolean result = sysUserService.checkUserNoExists(userNo);
        return R.success(result);
    }

    @GetMapping("/disableUser/{id}/{disableFlag}")
    @ApiOperation(value = "disableUser")
    public R<Boolean> disableUser(@PathVariable("id") String id,@PathVariable("disableFlag") Integer disableFlag){
        sysUserService.disableUser(id,disableFlag);
        return R.success(true);
    }

    @PostMapping("/saveUserCertificates")
    @ApiOperation(value = "saveUserCertificates")
    public R<List<SysUserCertificate>> saveUserCertificates(@RequestBody List<SysUserCertificate> list){
        list = sysUserCertificateService.saveUserCertificates(list);
        return R.success(list);
    }

    @PostMapping("/saveUserContracts")
    @ApiOperation(value = "saveUserContracts")
    public R<List<SysUserContract>> saveUserContracts(@RequestBody List<SysUserContract> list){
        list = sysUserContractService.saveUserContracts(list);
        return R.success(list);
    }

    @PostMapping("/saveUserContractPropertyList")
    @ApiOperation(value = "saveUserContractPropertyList")
    public R<List<UserContractListProperty>> saveUserContractPropertyList(@RequestBody List<UserContractListProperty> list){
        list = sysUserContractService.saveUserContractPropertyList(list);
        return R.success(list);
    }

    @PostMapping("/saveUserHealthReportPropertyList")
    @ApiOperation(value = "saveUserHealthReportPropertyList")
    public R<List<UserHealthReportListProperty>> saveUserHealthReportPropertyList(@RequestBody List<UserHealthReportListProperty> list){
        list = sysUserHealthReportService.saveUserHealthReportPropertyList(list);
        return R.success(list);
    }

    @PostMapping("/saveUserIdcards")
    @ApiOperation(value = "saveUserIdcards")
    public R<List<SysUserIdcard>> saveUserIdcards(@RequestBody List<SysUserIdcard> list){
        list = sysUserIdcardService.saveUserIdcards(list);
        return R.success(list);
    }

    @PostMapping("/saveUserHealthReport")
    @ApiOperation(value = "saveUserHealthReport")
    public R<List<SysUserHealthReport>> saveUserHealthReport(@RequestBody List<SysUserHealthReport> list){
        list = sysUserHealthReportService.saveUserHealthReport(list);
        return R.success(list);
    }

    @ApiOperation("saveUserRemark")
    @PostMapping("/saveUserRemark")
    public R<Boolean> saveUserRemark(@RequestBody JSONObject params){
        String userId = params.getString("userId");
        String remark = params.getString("remark");

        sysUserService.saveUserRemark(userId,remark);
        return R.success(true);
    }

    @ApiOperation("certificateListRemove")
    @PostMapping("/certificateListRemove")
    public R<Boolean> certificateListRemove(@RequestBody JSONObject params){
        String id = params.getString("id");
        sysUserCertificateService.removeById(id);

        return R.success(true);
    }

    @ApiOperation("contractListRemove")
    @PostMapping("/contractListRemove")
    public R<Boolean> contractListRemove(@RequestBody JSONObject params){
        String id = params.getString("id");
        sysUserContractService.removeById(id);
        return R.success(true);
    }

    @ApiOperation("idcardListRemove")
    @PostMapping("/idcardListRemove")
    public R<Boolean> idcardListRemove(@RequestBody JSONObject params){
        String id = params.getString("id");
        sysUserIdcardService.removeById(id);
        return R.success(true);
    }

    @ApiOperation("healthReportListRemove")
    @PostMapping("/healthReportListRemove")
    public R<Boolean> healthReportListRemove(@RequestBody JSONObject params){
        String id = params.getString("id");
        sysUserHealthReportService.removeById(id);
        return R.success(true);
    }

    @PostMapping("/getAllDepartNames")
    @ApiOperation("getAllDepartNames")
    public R<List<SysUser>> getAllDepartNames(){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT depart_name");
        return R.success(this.sysUserService.list(queryWrapper));
    }

    @PostMapping("/getAllRoleNames")
    @ApiOperation("getAllRoleNames")
    public R<List<SysUser>> getAllRoleNames(){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT role_name");
        return R.success(this.sysUserService.list(queryWrapper));
    }

    @GetMapping("/getUserContractProperties")
    @ApiOperation("getUserContractProperties")
    public R<List<UserContractListProperty>> getUserContractProperties(String userId){
        List<UserContractListProperty> contractProperties = sysUserContractService.getUserContractProperties(userId);
        return R.success(contractProperties);
    }

    @GetMapping("/removeUserRemarkById")
    @ApiOperation("removeUserRemarkById")
    public R removeUserRemarkById(String id){
        sysUserService.removeById(id);
        return R.success(true);
    }

    @PostMapping("/saveUserRemarkList")
    @ApiOperation("saveUserRemarkList")
    public R saveUserRemarkList(@RequestBody List<SysUserRemark> list){
        sysUserRemarkService.saveUserRemarkList(list);
        return R.success(list);
    }

    @PostMapping("/saveUserGrowLogList")
    @ApiOperation("saveUserGrowLogList")
    public R saveUserGrowLogList(@RequestBody List<SysUserGrowLog> list){
        sysUserGrowLogService.saveUserGrowLogList(list);
        return R.success(list);
    }

    @GetMapping("/removeUserGrowLogById")
    @ApiOperation("removeUserGrowLogById")
    public R removeUserGrowLogById(String id){
        sysUserGrowLogService.removeById(id);
        return R.success(true);
    }

    @PostMapping("/saveUserLeaveInfo")
    @ApiOperation("saveUserLeaveInfo")
    public R saveUserLeaveInfo(@RequestBody SysUserLeaveInfo sysUserLeaveInfo){
        sysUserLeaveInfoService.saveOrUpdate(sysUserLeaveInfo);
        return R.success(sysUserLeaveInfo);
    }

    @PostMapping("/saveUserRightIdList")
    @ApiOperation("保存用户权限")
    public R saveUserRightIdList(@RequestBody UserRightsVo userRightsVo){
        sysUser2RightService.saveUserRightIdList(userRightsVo);
        return R.success(true);
    }

    //获取所有人员列表
    @GetMapping("/getAllUserList")
    @ApiOperation("获取所有人员列表")
    public R<List<SysUser>> getAllUserList(){
        return R.success(sysUserService.list());
    }
}

